<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="warehousePlace" slot-scope="scope" >
        <el-link type="primary" @click="handleMap(scope.row.warehousePlace)">{{scope.row.warehousePlace.substring(scope.row.warehousePlace.lastIndexOf(',')+1)}}</el-link>
      </template>
      <template slot="oauth" slot-scope="scope" >
        <el-tag :type="scope.row.oauth===1?'danger':''">{{scope.row.oauth===1?'需要密码':'无需密码'}}</el-tag>
      </template>
    </avue-crud>
    <el-dialog :visible.sync="showMap">
      <avue-input-map  v-model="mapData" ></avue-input-map>
    </el-dialog>
    <el-dialog :visible.sync="showPassword">
      <avue-input-map  v-model="mapData" ></avue-input-map>
    </el-dialog>
    <el-dialog :visible.sync="showAdd">

    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove , getAll} from "@/api/warehouse/warehouse";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        mapData:[],
        showPassword:false,
        showMap:false,
        showAdd:false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {    excelBtn:true,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "仓库名",
              prop: "warehouseName",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "仓库地址",
              prop: "warehousePlace",
              type:"map",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "联系方式",
              prop: "warehouseOwner",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "类型",
              prop: "warehouseType",
              type: "select",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              slot: true,
              search:true,
              dicUrl: "/api/ocm-system/dict/dictionary?code=warehouse_type",
              rules: [{
                required: true,
                message: "请输入0 官方仓库 1个人仓库 2合作仓库",
                trigger: "blur"
              }]
            }, {
              label: "验证",
              prop: "oauth",
              type: 'select',
              search: true,
              dicData: [{
                title:'无需密码',
                value:0
              },{
                title:'需要密码',
                value:1
              }],
              props: {
                label: "title",
                value: "value"
              },
              rules: [{
                required: true,
                message: "请输入",
                trigger: "change"
              }]
            },
            {
              label: "描述",
              prop: "warehouseDescribe",
              hide:true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            }
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          viewBtn: this.vaildData(this.permission.all_view, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      handleMap(map){
        this.showMap=true
        this.mapData=map.split(',')
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getAll(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
